// pages/splendid/splendid.js
const app = getApp()
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1, //页数
    loading: true, //true可以加载 false不可以加载
  },
  /**
   * 点击上边导航改变点击类和swiper块
   */
  change: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    this.setData({
      swiperindex: index, //改变data里面的swiperindex
    })
  },
  /**
   * 返回最顶端
   */
  back_top:function(){
    this.setData({
      video_top:0
    })
  },
  /**
   * 滑动轮播图的时候改变index
   */
  changeswiper: function(e) {
    let that = this;
    let index = e.detail.current;
    this.setData({
      swiperindex: e.detail.current,
      page: 1, //初始化页数
    })
    if (index == 0) {
      that.setData({
        video_top: 0
      })
      that.getvideo()
    }
    if (index == 1) {
      that.setData({
        photo_top: 0
      })
      that.getphoto()
    }
    if (index == 2) {
      that.setData({
        photo_top: 0
      })
      that.getleave()
    }
  },
  /**
   * 请求列表接口
   */
  getvideo: function() {
    wx.showLoading({
      title: '加载中...',
    })
    let that = this;
    wx.request({
      url: app.httpUrl + '/activity/live/queryChoiceVideo',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      data: {
        "page": that.data.page,
        "rows": 10
      },
      method: 'POST',
      success: function(res) {
        // console.log(res.data.data.results)
        that.data.page += 1;
        that.setData({
          loading: false
        })
        var list = []; //储存数组
        that.data.video_list ? list = that.data.video_list.concat(res.data.data.results) : list = res.data.data.results
        if (that.data.page == 2) {
          list = res.data.data.results;
        }
        // for (let a = 0; a < list.length; a++) {
        //   for (let b = 0; b < list[a].attachList.length; b++) {
        //     list[a].attachList[b].play = true; //是否隐藏和播放域名
        //   }
        // }
        // console.log(list)
        that.setData({
          video_list: list,
        }, () => {
          if (that.data.page >= res.data.data.totalPage) {
            that.setData({
              loading: false,
            })
          } else {
            that.setData({
              loading: true
            })
          }
          wx.hideLoading()
        })
      }
    })
  },
  /**
   * 获取图片
   */
  getphoto: function() {
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    let that = this;
    wx.request({
      url: app.httpUrl + '/activity/live/queryChoiceImage',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      data: {
        "page": that.data.page,
        "rows": 10
      },
      method: 'POST',
      success: function(res) {
        that.data.page += 1;
        let photo_lits = [];
        that.data.photo_list ? photo_lits = that.data.photo_list.concat(res.data.data.results) : photo_lits = res.data.data.results
        if (that.data.page == 2) {
          photo_lits = res.data.data.results; //初始化数据
        }
        console.log(photo_lits)
        that.setData({
          photo_list: photo_lits,
        }, () => {
          if (that.data.page >= res.data.data.totalPage) {
            that.setData({
              loading: false,
            })
          } else {
            that.setData({
              loading: true
            })
          }
          wx.hideLoading()
        })
      }
    })
  },
  /**
   * 图片标题点击跳转活动中心
   */
  activityLive: function(event) {
    var activityId = event.currentTarget.dataset.idx;
    var childId = event.currentTarget.dataset.childid;
    app.globalData.activityId = activityId;
    app.globalData.childId = childId;
    wx.switchTab({
      url: '/pages/study/study'
    })
  },
  /**
   * 加载
   */
  loading: function(e) {
    let that = this;
    let index = that.data.swiperindex;
    let loading = that.data.loading;
    if (index == 0 && loading == true) {
      that.getvideo();
    }
    if (index = 1 && loading == true) {
      that.getphoto()
    }
    if (index = 2 && loading == true) {
      that.getleave()
    }
    if (loading == false) {
      wx.showToast({
        title: '没有更多...',
      })
    }
  },
  /**播放视频 */
  playing: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index; //外层下标
    let list = that.data.video_list[index];
    wx.setStorageSync('video', list);
    wx.navigateTo({
      url: '/pages/playvadio/playvadio',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    that.setData({
      swiperindex: options.index
    })
    let all_height = wx.getSystemInfoSync().windowHeight;
    // console.log(all_height)
    setTimeout(function() {
      var query = wx.createSelectorQuery();
      var listHeight; //获取头部导航高度
      query.select('.top').boundingClientRect()
      query.exec((res) => {
        // console.log(res)
        listHeight = res[0].height;
        that.setData({
          height: all_height - listHeight
        })
      })
    }, 200)

  },
  /**
   * 留言预览图片
   */
  seephotos:function(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let url = e.currentTarget.dataset.url;
    let list = that.data.leave_lis;
    let arr=[];//放图片的数组
    for(let a =0; a<list[index].attachList.length;a++){
      console.log(app.imgUrl + list[index].attachList[a].attachUrl)
      arr.push(app.imgUrl + list[index].attachList[a].attachUrl)
    }
    wx.previewImage({
      current: url, // 当前显示图片的http链接 
      urls: arr // 需要预览的图片http链接列表     
    })
  },
  /**
   * 预览图片
   */
  seephoto: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let list = that.data.photo_list;
    // console.log(list)
    let arr = []; //照片数组
    for (let a = 0; a < list[index].attachList.length; a++) {
      arr.push('http://img.xue110.top/taoxuequ' + list[index].attachList[a].attachUrl); //给图片加上域名
    }
    wx.previewImage({
      current: arr[0], // 当前显示图片的http链接 
      urls: arr // 需要预览的图片http链接列表     
    })
  },
  /**
   * 精选留言
   */
  getleave: function() {
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    let that = this;
    wx.request({
      url: app.httpUrl + '/activity/live/queryChoiceExpect',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      data: {
        "page": that.data.page,
        "rows": 10
      },
      method: 'POST',
      success: function(res) {
        that.data.page += 1;
        let leave_list = [];
        that.data.leave_lis ? leave_list = that.data.leave_lis.concat(res.data.data.results) : leave_list = res.data.data.results
        if (that.data.page == 2) {
          leave_list = res.data.data.results; //初始化数据
        }
        console.log(leave_list)
        for (let a = 0; a < leave_list.length; a++) {
          if (leave_list[a].nickName != null){
            leave_list[a].nickName = leave_list[a].nickName.replace(/\w\d+$/, '');
          }
          var relation;
          if (leave_list[a].childRelation == 1) {
            relation = "宝贝的爸爸";
          } else if (leave_list[a].childRelation == 2) {
            relation = "宝贝的妈妈";
          } else if (leave_list[a].childRelation == 3) {
            relation = "宝贝的亲戚";
          } else {
            relation = "宝贝父母的朋友";
          }
          leave_list[a].parentDesc = leave_list[a].childName + relation;
        }
        that.setData({
          leave_lis: leave_list,
        }, () => {
          if (that.data.page >= res.data.data.totalPage) {
            that.setData({
              loading: false,
            })
          } else {
            that.setData({
              loading: true
            })
          }
          wx.hideLoading()
        })
      }
    })
  },
  /**
   * 跳转课程详情
   */
  jump:function(e){
    let id = e.currentTarget.dataset.id;
    let linkType = e.currentTarget.dataset.linktype;
    wx.navigateTo({
      url: '/pages/programme/programme?productid=' + id + '&linkType=' + linkType,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getvideo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})